package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.LeadDocumentsVo;
import com.opgea.crm.domain.entities.LeadDocuments;
import com.opgea.crm.repository.LeadDocumentsRepo;

@Service
@Transactional
public class LeadDocumentServiceImpl implements LeadDocumentService{

	@Autowired
	LeadDocumentsRepo documentRepo;
	
	public List<LeadDocumentsVo> findAll() {
		List<LeadDocumentsVo> docList = new ArrayList<LeadDocumentsVo>();
		docList.add(new LeadDocumentsVo(1001, "PROPOSAL", "05/05/2011", 
										"ABC_PROPOSAL.doc", 345, "PROPOSAL, OFFER",
										"07/05/2011", "SHEKHAR"));
		return docList;
	}

	
	public void create(LeadDocuments documents) {
			documentRepo.create(documents);
	}


	
	public List<LeadDocumentsVo> findAllByLead(long leadId) {
		
		
		List<LeadDocuments> leadDocs = documentRepo.findAllByLead(leadId);
		List<LeadDocumentsVo> docList = new ArrayList<LeadDocumentsVo>();
		
		for(LeadDocuments doc : leadDocs){
			LeadDocumentsVo docVo = new LeadDocumentsVo();
			docVo.setDocumentId(doc.getId());
			docVo.setDocumentName(doc.getDocumentName());
			docVo.setFileName(doc.getFileName());
			docVo.setFileSize(doc.getFileSize());
			docVo.setReceiveDate(DateUtil.dateToString(doc.getReceiveDate()));
			//docVo.setUploadDate(DateUtil.dateToString(doc.getUploadedAt()));
			docVo.setDocumentType(ApplicationConstant.DocumentQualifier.values()[doc.getDocumentType()].toString());
			String content = new String(doc.getContent());
			docVo.setFileContent(content);
			//docVo.setUploadedBy(doc.getUploadedBy().getFirstName());
			docList.add(docVo);
			System.out.println("Document Vo : "+docVo);
		}
		return docList;
	}

}
